package com.shaoshaohuo.app.ui.fragment;

import java.util.ArrayList;
import java.util.List;

import android.os.Bundle;
import android.support.annotation.Nullable; 
import android.support.v4.app.Fragment;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;

import com.shaoshaohuo.app.R;

public class TestFragment extends Fragment {
	private ViewPager adViewPager;
	@Override
	@Nullable
	public View onCreateView(LayoutInflater inflater,
			@Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
		View view = (View) inflater.inflate(R.layout.test_fragment, null);
		adViewPager = (ViewPager) view.findViewById(R.id.adViewpager);
		AdPagerAdapter adPagerAdapter = new AdPagerAdapter(initAdPagerAdapterDatasource());
    	adViewPager.setAdapter(adPagerAdapter);
		return view;
	}
	private List<ImageView> initAdPagerAdapterDatasource() {
    	List<ImageView> images = new ArrayList<ImageView>();
    	int[] ids = {R.drawable.banner_ec_ad, R.drawable.banner_ec_ad};
    	for (int id : ids) {
    		ImageView iv = new ImageView(getActivity());
    		iv.setImageResource(id);
    		ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(  
                    ViewGroup.LayoutParams.MATCH_PARENT,  
                    ViewGroup.LayoutParams.MATCH_PARENT);  
            iv.setLayoutParams(params);  
            iv.setScaleType(ScaleType.FIT_XY); 
    		images.add(iv);
    	}
    	return images;
	}
	

    private class AdPagerAdapter extends PagerAdapter {
    	private List<ImageView> list;
    	
    	public AdPagerAdapter(List<ImageView> list) {
			super();
			this.list = list;
		}

		@Override
		public int getCount() {
			return list.size();
		}

		@Override
		public boolean isViewFromObject(View arg0, Object arg1) {
			return arg0 == arg1;
		}

		@Override
		public void destroyItem(ViewGroup container, int position, Object object) {
			container.removeView(list.get(position));
		}

		@Override
		public Object instantiateItem(ViewGroup container, int position) {
			container.addView(list.get(position));
			return list.get(position);
		}
    	
    }
}
